package com.smartcommunity.controller.fullSearch;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.smartcommunity.log.interceptor.Log;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.smartcommunity.bean.entity.User;
import com.smartcommunity.bean.enumeration.ResultCode;
import com.smartcommunity.bean.model.Pager;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.fullSearch.FullCountResponse;
import com.smartcommunity.entity.fullSearch.FullSearchRequest;
import com.smartcommunity.entity.fullSearch.FullSearchResponse;
import com.smartcommunity.entity.fullSearch.SearchHistory;
import com.smartcommunity.exception.BusinessException;
import com.smartcommunity.service.fullSearch.FullSearchService;
import com.smartcommunity.service.fullSearch.SearchHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: FullSearchController
 * @Description: 全息档案控制层
 * @author: yangyi
 * @Date: 2020/6/30 17:45
 * @Company:
 */
@RestController
@RequestMapping(value = "/fullSearch")
@Api(value = "全息档案", tags = "全息档案")
@Slf4j
public class FullSearchController extends BaseController {

	@Autowired
	private FullSearchService fullSearchService;
	@Autowired
	private SearchHistoryService searchHistoryService;
	@Autowired
    private HttpServletRequest request;
	
	@ApiOperation(value = "获取数据总数和今日新增数量", notes = "获取数据总数和今日新增数量")
	@PostMapping(value = "/count")
	@Log(info = "查询数据总数和今日新增数量",module = "全息档案",function = "查询")
	public Result fullCount() {
		log.info("接口：fullCount；全息档案-获取数据总数和今日新增数量");
		try {
			if(StringUtils.isEmpty(request.getParameter("villageCode"))) {
				return Result.ok(new FullCountResponse());
			}
			List<String> userVillageCode = Arrays.asList(request.getParameter("villageCode").replaceAll("'", "").split(","));
			FullCountResponse resp = fullSearchService.fullCount(userVillageCode, getCurrentUser().getId());
			return Result.ok(resp);
		} catch (BusinessException e) {
			log.error("全息档案-查询数据总量失败,e:", e);
			return Result.error(e.getCode(), e.getMessage());
		} catch (Exception e) {
			log.error("全息档案-查询数据总量失败,e:", e);
			return Result.error(ResultCode.FULLSEARCH_COUNT_ERROR.code(), ResultCode.FULLSEARCH_COUNT_ERROR.msg());
		}
	}
	
	@SuppressWarnings(value="unchecked")
	@ApiOperation(value = "全文检索查询", notes = "全文检索查询")
	@PostMapping(value = "/search")
	@Log(info = "全文检索查询",module = "全息档案",function = "查询")
	public Result<Pager<FullSearchResponse>> fullSearch(@RequestBody FullSearchRequest req) {
		log.info("接口：fullSearch；全息档案-全文检索");
		try {
			if(StringUtils.isEmpty(request.getParameter("villageCode"))) {
				log.warn("fullSearch；全息档案-全文检索villageCode is empty");
				return Result.ok(new Pager<>());
			}
			List<String> userVillageCode = Arrays.asList(request.getParameter("villageCode").replaceAll("'", "").split(","));
			Pager<FullSearchResponse> pager = fullSearchService.fullSearch(req, userVillageCode, getCurrentUser().getId());
			return Result.ok(pager);
		} catch (BusinessException e) {
			log.error("全息档案-全文检索失败,e:", e);
			return Result.error(e.getCode(), e.getMessage());
		} catch (Exception e) {
			log.error("全息档案-全文检索失败,e:", e);
			return Result.error(ResultCode.FULLSEARCH_SEARCH_ERROR.code(), ResultCode.FULLSEARCH_SEARCH_ERROR.msg());
		}
	}
	
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNumber" ,value = "页码"),
        @ApiImplicitParam(name = "pageSize" ,value = "每页数量")
	})
	@ApiOperation(value = "获取用户搜索历史", notes = "获取用户搜索历史")
	@SuppressWarnings("unchecked")
	@GetMapping(value = "/listUserHistory")
	@Log(info = "查询用户搜索历史信息",module = "全息档案",function = "查询")
	public Result<Pager<SearchHistory>> listUserHistory(@RequestParam(value="pageNumber", required=true)Integer pageNumber,
			@RequestParam(value="pageSize", required=true)Integer pageSize) {
		log.info("接口：listUserHistory；全息档案-获取用户搜索历史");
		User user = getCurrentUser();
		if(user == null) {
			return Result.error(ResultCode.USER_UNLOGIN_ERROR.code(),ResultCode.USER_UNLOGIN_ERROR.msg());
		}
		Pager<SearchHistory> pager = searchHistoryService.listUserHistory(pageNumber, pageSize, user.getId());
		return Result.ok(pager);
	}
	
	@ApiOperation(value = "删除用户搜索历史", notes = "删除用户搜索历史")
	@DeleteMapping(value = "/delUserHistory")
	@Log(info = "删除用户搜索历史信息",module = "全息档案",function = "删除")
	public Result delUserHistory() {
		log.info("接口：delUserHistory；全息档案-删除用户搜索历史");
		User user = getCurrentUser();
		if(user == null) {
			return Result.error(ResultCode.USER_UNLOGIN_ERROR.code(),ResultCode.USER_UNLOGIN_ERROR.msg());
		}
		searchHistoryService.delUserHistory(user.getId());
		return Result.ok();
	}
	
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id" ,value = "搜索历史ID")
	})
	@ApiOperation(value = "删除单条搜索历史", notes = "删除单条搜索历史")
	@DeleteMapping(value = "/delHistoryById")
	@Log(info = "删除用户单条搜索历史",module = "全息档案",function = "删除")
	public Result delHistoryById(@RequestParam(name = "id", required = true)Long id) {
		log.info("接口：delHistoryById；全息档案-删除单条搜索历史");
		searchHistoryService.delHistoryById(id);
		return Result.ok();
	}
	
}
